/**
 * Project Name:Android_Car_Example
 * File Name:LocationTask.java
 * Package Name:com.amap.api.car.example
 * Date:2015年4月3日上午9:27:45
 */

package com.gears_software.sxdc.dccx.ui.map;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.LocationSource;
import com.gears_software.sxdc.dccx.data.model.PositionEntity;
import com.gears_software.sxdc.dccx.ui.activity.MainActivity;
import com.gears_software.sxdc.dccx.ui.fragment.SpecialCarFragment;
import com.gears_software.sxdc.dccx.ui.fragment.SpecialCarFragment_;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;


/**
 * ClassName:LocationTask <br/>
 * Function: 简单封装了定位请求，可以进行单次定位和多次定位，注意的是在app结束或定位结束时注意销毁定位 <br/>
 * Date: 2015年4月3日 上午9:27:45 <br/>
 *
 * @author yiyi.qi
 * @see
 * @since JDK 1.6
 */
public class LocationTask implements AMapLocationListener,
        OnLocationGetListener {

    private AMapLocationClient mLocationClient;

    private static LocationTask mLocationTask;

    private Context mContext;

    private OnLocationGetListener mOnLocationGetlisGetListener;
    private RegeocodeTask mRegecodeTask;

    public LocationTask(Context context) {
        mLocationClient = new AMapLocationClient(context);
        mLocationClient.setLocationListener(this);
        mRegecodeTask = new RegeocodeTask(context);
        mRegecodeTask.setOnLocationGetListener(this);
        mContext = context;
    }

    public void setOnLocationGetListener(
            OnLocationGetListener onGetLocationListener) {
        mOnLocationGetlisGetListener = onGetLocationListener;
    }

//    public static LocationTask getInstance(Context context) {
//        if (mLocationTask == null) {
//            mLocationTask = new LocationTask(context);
//        }
//        Logger.d(mLocationTask);
//        return mLocationTask;
//    }

    /**
     * 开启单次定位
     */
    public void startSingleLocate() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(true);
        option.setOnceLocationLatest(true);
        option.setInterval(2000);
        mLocationClient.setLocationOption(option);
        mLocationClient.startLocation();

    }

    /**
     * 开启多次定位
     */
    public void startLocate(int sec) {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(false);
        option.setInterval(sec * 1000);
        mLocationClient.setLocationOption(option);
        mLocationClient.startLocation();

    }

    /**
     * 结束定位，可以跟多次定位配合使用
     */
    public void stopLocate() {
        mLocationClient.stopLocation();

    }

    /**
     * 销毁定位资源
     */
    public void onDestroy() {
        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
        mLocationTask = null;
    }


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null && amapLocation.getErrorCode() == 0) {
            PositionEntity entity = new PositionEntity();
            entity.setLatitue(amapLocation.getLatitude());
            entity.setLongitude(amapLocation.getLongitude());
            entity.setCountry(amapLocation.getCountry());
            entity.setProvince(amapLocation.getProvince());
            entity.setCity(amapLocation.getCity());
            entity.setDistrict(amapLocation.getDistrict());
            entity.setStreet(amapLocation.getStreet());
            entity.setStreetNum(amapLocation.getStreetNum());
            entity.setCityCode(amapLocation.getCityCode());
            entity.setAdCode(amapLocation.getAdCode());
            if (!TextUtils.isEmpty(amapLocation.getAddress())) {
                entity.setAddress(amapLocation.getAddress());
            }
            mOnLocationGetlisGetListener.onLocationGet(entity, amapLocation);
        } else {
            Logger.d("AmapError gps Error ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo());
//            TastyToast.makeText(mContext, "AmapError gps Error ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo(),
//                    TastyToast.LENGTH_LONG, TastyToast.ERROR);
        }
    }

    @Override
    public void onLocationGet(PositionEntity entity, AMapLocation aMapLocation) {

        // TODO Auto-generated method stub


    }

    @Override
    public void onRegecodeGet(PositionEntity entity) {

        // TODO Auto-generated method stub

    }
}
